
package Project4LineEdit;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;



public class LineEditorControl extends LineList {

	/*
	 * This contains all the text of the line editor
	 */
	public ArrayList<LineList> MyDoc = new ArrayList<LineList>();
	
	/*
	 * This points to the Current Line in the editor
	 */
	public int CurLine  = -1;
	/*
	 *
	 *  $insert Text
	 *  
	 *  $delete m n
	 *  	m - Starting Line
	 *  	n - Ending Line
	 *  
	 *  $print m n
	 * 		m - Starting Line
	 *  	n - Ending Line
	 *  	if not paramaters given print all Lines
	 *  
	 *  $line m
	 *  	m - set line m as the active line
	 * 
	 *	$search S
	 *		S - String to search for in the text
	 *
	 *	$done 
	 *		Terminates Application
	 * 
	 * 
	 */
	
	public void ProcessInput(String FileName)
	{
		boolean Done = false;
		Scanner MyScanner = null;
		try {
			MyScanner = new Scanner(new File(FileName));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.getMessage());
		}

		String NextLine = "";
		boolean LineProcessed = false;
		boolean Inserting = false;
		String CurrentCMD  = "";
		String Parm1 = "";
		String Parm2 = "";
		
		while (!Done)
		{
		try
		{
			if (MyScanner.hasNextLine() == true)
			{
			NextLine = MyScanner.nextLine();
			//System.out.println("DEBUG:NEWLINE: " + NextLine);
			CurrentCMD = "";
			Parm1 = null;
			Parm2 = null;
		
			if (NextLine.trim().startsWith("$"))
			{
				//System.out.println("DEBUG:NEW CMD");
				
				WordList MyCmdList = new WordList();
				MyCmdList = CreateNewWordList(NextLine);
				CurrentCMD = MyCmdList.Word;
				if (MyCmdList.NextWord != null)
				{
					MyCmdList = MyCmdList.NextWord ;

					if (MyCmdList.Word.indexOf(",") > -1)
					{ 
						Parm1 = MyCmdList.Word.substring(0,MyCmdList.Word.indexOf(","));
						Parm2 = MyCmdList.Word.substring(MyCmdList.Word.indexOf(",")+ 1);
					}
					else
					{
						Parm1 = MyCmdList.Word;
					}
				}
				Inserting = false;
			}
			if (Inserting)
			{
				AddLine(NextLine);
			}
			else
			{
				LineProcessed = false;
				//read next Line
				//if Insert
				//read and add to lists until next cmd
				if 	(CurrentCMD.toLowerCase().equals("$insert"))
				{
					Inserting = true;
					LineProcessed = true;				
				}
	
				//if Delete get Delete Paramaters Call Delete
				if 	(CurrentCMD.toLowerCase().equals("$delete"))
				{
					Delete(Integer.parseInt(Parm1),Integer.parseInt(Parm2));
					LineProcessed = true;				
				}
				//if Print Get Print Paramaters Call print
				if 	(CurrentCMD.toLowerCase().equals("$print"))
				{
					if (Parm1 == null)
						Parm1 = "-1";
					if (Parm2 == null)
						Parm2 = "-1";
					Print(Integer.parseInt(Parm1),Integer.parseInt(Parm2));
					LineProcessed = true;				
				}
				//if Line get Paramater Call Line
				if 	(CurrentCMD.toLowerCase().equals("$line"))
				{
					Line(Integer.parseInt(Parm1));
					LineProcessed = true;				
				}		
				//if Search get Paramater Call Search
				if 	(CurrentCMD.toLowerCase().equals("$search"))
					{
					Search(Parm1);	
					LineProcessed = true;				
					}
				//if Done End Program
				if 	(CurrentCMD.toLowerCase().equals("$done"))
				{
					Done = true;
					LineProcessed = true;
				}
				if (!LineProcessed)
				{
					System.out.println(NextLine + " is not a valid Command");
				}
			}
			}
			else
			{
				Done = true;
			}
		}
		catch (Exception ex)
		{
			System.out.println("ERROR " + ex.getMessage());
		}

		}
		
		MyScanner.close();
	}
	
	public void AddLine(String NewLine)
	{

			LineList Cur = CreateNewLineList(NewLine);
			if (CurLine < 0)
				MyDoc.add(Cur);				
			else
			{
				MyDoc.add(CurLine,Cur);
				CurLine = CurLine + 1;
			}
	}
	
	public LineList CreateNewLineList(String NewLine)
	{
		LineList MyLL = new LineList();
		
		MyLL.Words = CreateNewWordList(NewLine);
		
		return MyLL;
	}
	
	public WordList CreateNewWordList(String NewLine)
	{
		NewLine = NewLine.trim();
		if (NewLine.equals(""))
		{
			return null;
		}
		else
		{
			WordList MyWL = new WordList();
	
			
			int SpaceIndex = NewLine.indexOf(" ");

			if (SpaceIndex == -1)
			{
				SpaceIndex =  NewLine.length();
			}
			
			MyWL.Word = NewLine.substring(0, SpaceIndex);
			MyWL.NextWord = CreateNewWordList(NewLine.substring(SpaceIndex));
			
			return MyWL;
		}
	
	}
	
	public void Search(String SearchString) throws Exception
	{
		WordList iWord = null;
		boolean Found = false;

		for(int i = 0; i < MyDoc.size(); i++)
		{
			iWord = MyDoc.get(i).Words ;
			while (iWord != null)
			{
				if (iWord.Word.toLowerCase().equals( SearchString.toLowerCase()))
				{
					 MyDoc.get(i).Print();
					Found = true;
				}
				iWord = iWord.NextWord;
			}
		}
	
		if (!Found)
			throw new Exception(SearchString + " Not Found");
	}
	
	public void Delete(int M, int N) throws Exception
	{
		//Check for valid M and N values
		if ((N - M) < 0)
		{
			throw new Exception("Endline cant be less then the StartLine");
		}

		if (N > MyDoc.size())
		{
			throw new Exception("Endline Null Ref");
		}
		
		for(int i = N - 1; i>=M - 1; i--)
		{
			MyDoc.remove(i);
		}

		/*
		 * 
		LineList LineZero = MyDoc;
		LineList NextNode = MyDoc;
		//validates all lines
		for (int j = 0; j < N; j++)
		{
			if (MyDoc.NextLine == null)
			{
				//Throw an exception if line is null	
				throw new Exception("Line " + j+1 + " is Null");
			}
			else
			{		
				//Gets to the Starting Line
				if (j < M)
					MyDoc = MyDoc.NextLine ;	
				if (j < N)
					NextNode = NextNode.NextLine;
			}
		}
		MyDoc.NextLine = NextNode;
		
		MyDoc = LineZero;
		
		 */
	}
	
	public void Print(int M, int N) throws Exception
	{
		if (N == -1 && M == -1)
		{
			//Print All
			for(int i = 0; i < MyDoc.size(); i++)
			{
				MyDoc.get(i).Print();
			}
		}
		else
		{
			//Check for valid M and N values
			if ((N - M) < 0)
			{
				throw new Exception("Endline cant be less then the StartLine");
			}

			if (N > MyDoc.size())
			{
				throw new Exception("Endline Null Ref");
			}
			
			for(int i = M - 1; i <= N - 1; i++)
				MyDoc.get(i).Print();
		}
			//Print between M and N
	}
	

	public void Line(int M) throws Exception
	{
		CurLine = M - 1;
		
		for(int i = M - 4; i <= M + 2; i++)
		{
			if (i >= 0)
			{
				if (i < MyDoc.size())
				{
					MyDoc.get(i).Print();
				}
			}
		}
	}

}
